package io;

import java.io.File;
import java.util.Scanner;

public class IODemo13 {
    public static void main(String[] args) {
        //1.输入必要信息
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的文件名:");
        String filename = scanner.next();
        System.out.println("请输入要搜索的目录");
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()) {
            System.out.println("输入的路径有误!");
            return;
        }

        //2.按照递归的方式来搜索
        scanDir(rootFile,filename);
    }

    private static void scanDir(File rootFile,String filename) {
        //1.把当前目录中的文件和子目录都列出来
        System.out.println("当前遍历路径:" + rootFile.getAbsolutePath());

        File[] files = rootFile.listFiles();
        if(files == null) {
            return;
        }

        //2.遍历上述 files, 判定每一个file是目录还是文件
        for(File f:files) {
            if(f.isFile()) {
                if(filename.equals(f.getName())) {
                    System.out.println("找到了符合要求的文件!  " + f.getAbsolutePath());
                    //return;
                }
            }else if(f.isDirectory()) {
                scanDir(f,filename);
            }else {
                //这里啥也不干
            }
        }
    }
}
